package de.hsos.mad.aufg05_cameraapp;

import java.io.FileNotFoundException;
import java.io.IOException;

import com.example.aufg05_cameraapp.R;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

	// Voransicht der Kamera
	private ImageView imageView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Landscape Modus einstellen
		setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
		setContentView(R.layout.activity_main);
		imageView = (ImageView) findViewById (R.id.imageView);
		
		// Listener zum Starten der Aufnahme
		Button button = (Button) findViewById (R.id.btnShoot); 
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) { 
				startCamera();
			}
		});
	}
	
	// Speicherung der Aufnahme unter dieser URI
	private Uri imageUri; 
	private void startCamera() {
		ContentValues values = new ContentValues(); 
		values.put(MediaStore.Images.Media.TITLE, "Greetings_from_OS"); 
		values.put(MediaStore.Images.Media.DESCRIPTION, "Gruß aus Osnabrück");
		values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
		imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
		
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
		startActivityForResult(intent, IMAGE_CAPTURE_CODE);
	}
	
	// Synchronisation zwischen dieser und der Kamera-Aktivität
	private static final int IMAGE_CAPTURE_CODE = 1;
	protected void onActivityResult (int requestCode, int resultCode, Intent data) {
		if (requestCode == IMAGE_CAPTURE_CODE) {
			if (resultCode == RESULT_OK) {
				try {
					Bitmap b1 = MediaStore.Images.Media.getBitmap( getContentResolver(), imageUri);
					// Größe des aufgenommenen Bildes
					float w1 = b1.getWidth();
					float h1 = b1.getHeight();
					// auf eine Höhe von 300 Pixel skalieren
					int h2 = 300;
					int w2 = (int) (w1 / h1 * (float) h2);
					Bitmap b2 = Bitmap.createScaledBitmap(b1, w2, h2, false);
					
					imageView.setImageBitmap(b2); 
				} catch (FileNotFoundException e) {
					System.out.println ("setBitmap()" + e); 
				} catch (IOException e) {
					System.out.println ("setBitmap()" + e);
				}
			}
			else {
				int rowsDeleted = getContentResolver().delete (imageUri, null, null);
				System.out.println (rowsDeleted + " rows deleted");
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
